package com.qk.service.impl;

import com.qk.mapper.BusinessMapper;
import com.qk.mapper.ClueMapper;
import com.qk.service.ReportService;
import com.qk.vo.OverviewVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Slf4j
@Service
public class ReportServiceImpl implements ReportService {
    @Autowired
    private BusinessMapper businessMapper;
    @Autowired
    private ClueMapper clueMapper;
    @Autowired
    private RedisTemplate<Object, Object> redisTemplate;

    @Override
    public OverviewVo getOverview() {
        //1.从redis中获取数据
        log.info("从redis中获取数据");
        Object overviewData = redisTemplate.opsForValue().get("OVERVIEW_DATA");
        if (overviewData != null) {
            log.info("从redis中获取数据成功");
            return (OverviewVo) overviewData;
        }
        log.info("从redis中获取数据失败");
        //2.缓存中没有数据,从数据库中获取数据
        //2.1获取线索概要
        OverviewVo clueOverview = clueMapper.getClueOverview();
        //2.2获取商机概要
        OverviewVo businessOverview = businessMapper.getBusinessOverview();
        //2.3将线索概要和商机概要合并
        BeanUtils.copyProperties(businessOverview, clueOverview, "clueTotal", "clueWaitAllot", "clueWaitFollow", "clueFollowing", "clueFalse", "clueConvertBusiness");
        //3.将数据保存到redis中,缓存过期时间1分钟
        redisTemplate.opsForValue().set("OVERVIEW_DATA", clueOverview, 1, TimeUnit.MINUTES);
        log.info("将数据保存到redis中");
        return clueOverview;

    }
}
